import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/login")
public class IndexServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String userName = req.getParameter("userName");
        String passWard = req.getParameter("passWard");
        if(!userName.equals("xiaosan") && !userName.equals("xiaoyang")){
            //登录失败
            System.out.println("用户名错误，请重新输入！");
            resp.sendRedirect("Login.html");
            return;
        }
        if(!passWard.equals("123456")){
            System.out.println("密码错误，请重新输入！");
            resp.sendRedirect("Login.html");
            return;
        }

        //登录成功
        //1，创建一个会话
        HttpSession session = req.getSession(true);
        // 2，把当前的用户名保存到会话中，此处HttpSession可以当作一个map使用
        session.setAttribute("userName",userName);
        // 3，重定向到主页
        resp.sendRedirect("index");

    }
}
